"""
open(文件路径,mode="",encoding="")
    文件路径
        1、绝对路径
            d:/test/xxx.txt
        2.相对路径
            ./xxx.txt
    mode:
        r: read 读取
        w: write 写入
        a: append 追加
        b: 非文本文件

with 语句用于上下文管理，确保资源在使用后正确释放。它常用于文件操作、锁等场景，自动处理打开和关闭资源的过程，简化代码并提高安全性。
"""

f = open('你好世界.txt', mode='r', encoding='utf-8')
# content = f.read()
# print(content)
#
#
# line = f.readline().strip()
# print(line)
# line = f.readline()
# print(line)
# line = f.readline()
# print(line)

content = f.readlines()
print(content)

for line in content:
    print(line.strip())


# f1.write('新内容1')

f1 = open('新的文件.txt', mode='w',encoding='utf-8')
lst = ['新内容1','新内容2','新内容3']
# f1.write(lst[0])
for i in lst:
    f1.write(i)
    f1.write('\n')

f1.writelines(lst)

f2 = open('新的文件2.txt', mode='a', encoding='utf-8')
f2.write('瞧瞧')

f.close()
f1.close()
f2.close()

print("===============")

with open('你好世界.txt', mode='r', encoding='utf-8') as f:
    for i in f:
        print(i.strip())

with open('礼盒.png', mode='rb',) as img:
    for line in img:
        print(line)

with open('礼盒.png', mode='rb') as img1, \
     open('../01base/礼盒2.jpg', mode='wb') as f2:
        for line in img1:
            f2.write(line)



with open('名单.txt', mode='r', encoding='utf-8') as ff,\
    open('名单2.txt', mode='w', encoding='utf-8') as ff2:
    for line in ff:
        line = line.strip()
        if line.startswith('周'):
            line = line.replace('周', '粥')
        ff2.write(line)
        ff2.write('\n')

# 删除原文件 改名新文件

import os
import time

time.sleep(2)
os.remove('名单.txt')

time.sleep(2)
os.rename('名单2.txt','名单.txt')


